<template>
  <div>
		<van-nav-bar
			title="找回密码"
			left-text="返回"
			left-arrow
			fixed
			@click-left="$router.go(-1)"
		/>
		
		<van-form @submit="onforgetPsw" >
			<userInfo ref="userRef" :passportType="passportType"></userInfo>
			
			<div style="margin: 16px;">
				<van-button round block type="info" native-type="submit">找回密码</van-button>
			</div>
		</van-form>

  </div>
</template>

<script>
import { Toast } from 'vant';
import userInfo from '@/components/userInfo.vue'
import { findPsw } from '@/api/passwd.js';
export default {
  name: 'forgetPsw',
  components: {
    userInfo
  },
	data() {
		return {
				passportType: 'forgetPsw',
		}
	},
	methods: {
		onforgetPsw() {  //  找回密码
			
			
			// 密码两次是否正确
			if(this.$refs.userRef.password === this.$refs.userRef.ppassword) {
				
				const pswInfo = {
					"code": this.$refs.userRef.sms,
					"password": this.$refs.userRef.password,
					"phone": this.$refs.userRef.username
				}
				findPsw(pswInfo).then(res => {
					console.log(res);
					if( res.data.code === 0) { // 成功
						Toast.loading({
							message: '加载中...',
							forbidClick: true,
						});
						const timer = setInterval(() => {
							this.$router.push('login');					
							Toast('密码找回成功');
							clearInterval(timer);
						}, 2000);
					} else {
						Toast('找回密码失败，请重试！');
					}
				})
			} else {
				Toast('输入两次密码不一样，请重新输入');
			}
		}
	}
}
</script>
	
<style>

</style>